<?php
include "Project.php";

class ProjectDAO extends DataLayer {
	public		$statusNames;
 		
	/**
	* @desc Construtor da Classe
 	*/
	function __construct($lk){
		$this->lk = $lk;
		$this->statusNames = array("Deletado","Em Andamento","Finalizado","Arquivado");
	}

	public function getProjectsMaster($id=0, $page=0, $status=NULL, $idUser, $search=""){

		if($status == NULL)
			$status =  array(1,2);
	
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							p.id_projeto, p.id_product_owner, p.id_scrum_master, p.nome, p.cliente, p.url_agenda_google, p.descricao, DATE_FORMAT(p.data_inicial, '%d/%m/%Y') data_inicial, DATE_FORMAT(p.data_fim_estimada, '%d/%m/%Y') data_fim_estimada, DATE_FORMAT(p.data_fim_real, '%d/%m/%Y') data_fim_real, p.data, count(s.id_sprint) total_sprints, DATE_FORMAT(p.data, '%d/%m/%Y') data, p.status
						FROM 
							projetos p
						LEFT JOIN 
							sprints s on s.id_projeto = p.id_projeto
						LEFT JOIN 
							equipes e on e.id_equipe = s.id_equipe
						LEFT JOIN 
							equipes_usuarios eu on eu.id_equipe = e.id_equipe
						WHERE 
							p.status IN (".implode(",",$status).") AND (eu.id_usuario = ".$idUser." OR p.id_scrum_master = ".$idUser.")";

		if($id > 0)						
			$this->sql .= " and p.id_projeto = ".$id;

		if($search != "")
			$this->sql .= " and p.nome LIKE '%".$search."%'";
		
		$this->sql .= " GROUP BY p.id_projeto ORDER BY p.data_fim_estimada ASC, p.id_projeto DESC";

		return $this->pagination($page);
	}

	public function getProjectsNormalUser($id=0, $page=0, $status=NULL, $idUser, $search=""){

		if($status == NULL)
			$status =  array(1,2);
	
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							p.id_projeto, p.id_product_owner, p.id_scrum_master, p.nome, p.cliente, p.url_agenda_google, p.descricao, DATE_FORMAT(p.data_inicial, '%d/%m/%Y') data_inicial, DATE_FORMAT(p.data_fim_estimada, '%d/%m/%Y') data_fim_estimada, DATE_FORMAT(p.data_fim_real, '%d/%m/%Y') data_fim_real, p.data, count(s.id_sprint) total_sprints, DATE_FORMAT(p.data, '%d/%m/%Y') data, p.status
						FROM 
							projetos p
						LEFT JOIN 
							sprints s on s.id_projeto = p.id_projeto
						LEFT JOIN 
							equipes e on e.id_equipe = s.id_equipe
						LEFT JOIN 
							equipes_usuarios eu on eu.id_equipe = e.id_equipe
						WHERE 
							p.status IN (".implode(",",$status).") AND eu.id_usuario = ".$idUser;

		if($id > 0)						
			$this->sql .= " and p.id_projeto = ".$id;

		if($search != "")
			$this->sql .= " and p.nome LIKE '%".$search."%'";
		
		$this->sql .= " GROUP BY p.id_projeto ORDER BY p.data_fim_estimada ASC, p.id_projeto DESC";

		return $this->pagination($page);
	}

	public function getProjects($id=0, $page=0, $status=NULL, $search=""){

		if($status == NULL)
			$status =  array(1,2);
	
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							p.id_projeto, p.id_product_owner, p.id_scrum_master, p.nome, p.cliente, p.url_agenda_google, p.descricao, DATE_FORMAT(p.data_inicial, '%d/%m/%Y') data_inicial, DATE_FORMAT(p.data_fim_estimada, '%d/%m/%Y') data_fim_estimada, DATE_FORMAT(p.data_fim_real, '%d/%m/%Y') data_fim_real, p.data, count(s.id_sprint) total_sprints, DATE_FORMAT(p.data, '%d/%m/%Y') data, p.status
						FROM 
							projetos p
						LEFT JOIN 
							sprints s on s.id_projeto = p.id_projeto
						WHERE 
							p.status IN (".implode(",",$status).")";

		if($id > 0)						
			$this->sql .= " and p.id_projeto = ".$id;

		if($search != "")
			$this->sql .= " and p.nome LIKE '%".$search."%'";
		
		$this->sql .= " GROUP BY p.id_projeto ORDER BY p.data_fim_estimada ASC, p.id_projeto DESC";

		return $this->pagination($page);
	}

	private function pagination($page=0){
		if($page > 0){
			$page = (int)$page;
			$page--;
			if($page < 0)
				$page = 0;
			else
				$page *= PAGINATION;

			$this->sql .= " LIMIT ".$page.",".PAGINATION;
		}
		
		$this->result = parent::select($this->sql);
		$projectList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$projectList[$i] = new Project();				
				$projectList[$i]->setDadosDB($row);				

				$i++;
			}
		}
		
		return $projectList;
	}

	public function save(Project $obj){
		$return = 0;

		if($obj != NULL){
			$dbObj = $obj->fieldsDBObj;
			unset($dbObj["id_projeto"]);

			if($obj->id > 0){
				parent::update("projetos",$dbObj,"id_projeto = ".$obj->id,"data");
				$return = $obj->id;
			}else{
				$idProject = parent::insert("projetos",$dbObj,"data");
			
				if($idProject > 0){
					$objTaskDAO	= new TaskDAO($this->lk);
					$objTaskDAO->createTasksState($idProject);
				}

				$return = $idProject;
			}
		}

		return $return;
	}

	public function setEndProject($idProject, $archive=false){
		if($idProject > 0){
			$project = new Project();
			$project->status = 2;
			$project->dateEndReal = date("Y-m-d H:i:s");

			parent::update("projetos",$project->fieldsDBObj,"id_projeto = ".$idProject,"data");

			return true;
		}

		return false;
	}

	public function archiveProject($id){
		if($id > 0){
			$rsProject = $this->getProjects($id,0);

			if(count($rsProject) > 0){
				$dados["status"] = 3;
				parent::update("projetos", $dados, "id_projeto=".$id, "data");

				$result = $this->lk->sql_error();

				if($result["code"] == 0){
					return true;
				}
					
			}
		}

		return false;
	}


	public function deleteProject($id=0){
		if($id > 0){
			$rsProject = $this->getProjects($id,0);

			if(count($rsProject) > 0){
				$dados["status"] = 0;
				parent::update("projetos", $dados, "id_projeto=".$id, "data");

				$result = $this->lk->sql_error();

				if($result["code"] == 0){
					return true;
				}
					
			}
		}

		return false;
	}
}

?>